package com.mine.site.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mine.site.dao.ShoppingCartDao;
import com.mine.site.model.ShoppingCart;
import com.mine.site.model.User;





@SuppressWarnings("unused")
@WebServlet("/ShoppingCartServlet")
public class ShoppingCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ServletContext session;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("utf-8");
		
		
		HttpSession session = request.getSession();
		User u = (User)session.getAttribute("loginUser");
		if(u == null){    //���û�Ϊ��ʱ��չ��ﳵ���
			response.sendRedirect("login.jsp");
			return;
		}
		ServletContext ctx = request.getServletContext();

		PrintWriter out =  response.getWriter();
		String Name = request.getParameter("Name");
		String method = request.getParameter("m");
		if("del".equals(method)){
			this.del(request, response);
		}else if("forEdit".equals(method)){
			this.beforeEdit(request, response);
		}	else if("byname".equals(method)){
				this.byname(request, response);
		}else if("update".equals(method)){
			this.update(request, response);
		}
		else if("save".equals(method)){
			this.save(request, response);
		}
		else if("goumai".equals(method)){
			this.goumai(request, response);
		}
		else{
			this.list(request, response);
		}
	}



	private void goumai(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ShoppingCart u = wrapUser(request);
		
		ShoppingCartDao dao = new ShoppingCartDao();
		dao.save(u);
		response.sendRedirect("ShoppingCartServlet");
		
	}



	private void byname(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String Name = request.getParameter("Name");
		
		ShoppingCartDao dao = new ShoppingCartDao();
		List<ShoppingCart> cart = dao.findByName(Name);
		request.setAttribute("cartlist", cart);	
		request.getRequestDispatcher("cart.jsp").forward(request, response);
		
	}



	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ShoppingCartDao dao = new ShoppingCartDao();
		List<ShoppingCart> cart = dao.findAll();
		
		request.setAttribute("cartlist", cart);	
		
		request.getRequestDispatcher("cart.jsp").forward(request, response);
	}

	private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ShoppingCart u = wrapUser(request);
		ShoppingCartDao dao = new ShoppingCartDao();
		dao.save(u);
		response.sendRedirect("addcart.jsp");
		
	}

	private void update(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		
	}

	private void beforeEdit(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		
	}

	private void del(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String Url = request.getParameter("Url");
		ShoppingCartDao dao = new ShoppingCartDao();
		dao.delete(Url);
		response.sendRedirect("ShoppingCartServlet");
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	private ShoppingCart wrapUser(HttpServletRequest request) {
		ShoppingCart u = new ShoppingCart();
		u.setId(request.getParameter("Id"));
		u.setName(request.getParameter("Name"));
		u.setNumber(request.getParameter("Number"));
		u.setTitle(request.getParameter("Title"));
		u.setPrice(request.getParameter("Price"));
		u.setUrl(request.getParameter("Url"));
		return u;
	}
}
